VBA script til Word for å endre på sidenummerering

Endre på sidenummerering i flettet dokument med inndelingsskift

Kilde: https://wordribbon.tips.net/T013546_Controlling_Page_Numbers_in_Mail-Merged_Documents.html

Denne koden endrer til sammenhengende nummerering i alle seksjoner og oppdater innholdsfortegnelsen. (Linjer kommenteres med ’)

Sub FixPageNumbersAndTOC()
    Dim oSection As Section
    Dim oFooter As HeaderFooter
    Dim oHeader As HeaderFooter
    Dim oTOC As TableOfContents

    For Each oSection In ActiveDocument.Sections
        For Each oFooter In oSection.Footers
            oFooter.PageNumbers.RestartNumberingAtSection = False
        Next oFooter
        For Each oHeader In oSection.Headers
            oHeader.PageNumbers.RestartNumberingAtSection = False
        Next oHeader
    Next oSection

    ActiveDocument.Repaginate

    For Each oTOC In ActiveDocument.TablesOfContents
        oTOC.Update
    Next oTOC
End Sub

Konvertere html-tags til tekststyling

Sub ConvertHTMLTags()
    Call ByttTilFormat("\<b\>(*)\<\/b\>", "fet")
    Call ByttTilFormat("\<strong\>(*)\<\/strong\>", "fet")
    Call ByttTilFormat("\<i\>(*)\<\/i\>", "kursiv")
    Call ByttTilFormat("\<u\>(*)\<\/u\>", "understrek")
    Call ByttTilFormat("\<s\>(*)\<\/s\>", "gjennomstrek")
End Sub

Sub ByttTilFormat(mønster As String, format As String)
    Dim rng As Range
    Set rng = ActiveDocument.Content

    With rng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = mønster
        .Replacement.Text = "\1"
        .MatchWildcards = True
        .Forward = True
        .Wrap = wdFindStop

        Select Case format
            Case "kursiv":     .Replacement.Font.Italic = True
            Case "fet":        .Replacement.Font.Bold = True
            Case "understrek": .Replacement.Font.Underline = wdUnderlineSingle
            Case "gjennomstrek": .Replacement.Font.StrikeThrough = True
        End Select

        .Execute Replace:=wdReplaceAll
    End With
End Sub

Last modified: Sat Oct 10 11:00:07 2026